﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC.ComboBox
{
    public partial class User : UserControl
    {
        List<Model.DB.tb_User> _ListAll, _ListSearch;
        public User()
        {
            InitializeComponent();

            cmbUser.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cmbUser.DropDownStyle = ComboBoxStyle.DropDown;
            cmbUser.DisplayMember = "userName";
            cmbUser.ValueMember = "userId";

            _ListAll = new DataAccess.tb_User().getAll().ToList();

            cmbUser.Items.AddRange(_ListAll.ToArray());
            //cmbUser.DataSource = _ListAll;

            cmbUser.TextUpdate += CmbUser_TextUpdate;

            //cmbUser.DropDownClosed += CmbUser_DropDownClosed;

            //cmbSumitUnit.LostFocus += CmbSumitUnit_DropDownClosed;

            if (cmbUser.Items.Count > 0)
            {
                cmbUser.SelectedIndex = 0;
            }
        }

        private void CmbUser_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                System.Threading.Thread.Sleep(100);
                this.Invalidate(true);
                //if (_ListAll.Count < 0)
                //    return;

                //for (int i = 0; i < _ListSearch.Count; i++)
                //{
                //    if (_ListSearch[i].userName == cmbSumitUnit.Text)
                //    {
                //        cmbSumitUnit.SelectedIndex = i;
                //        return;
                //    }
                //}

                //if (cmbSumitUnit.Items.Count > 0)
                //{
                //    cmbSumitUnit.SelectedIndex = 0;
                //}
                //else
                //{
                //    cmbSumitUnit.Items.Clear();
                //    cmbSumitUnit.Items.AddRange(_ListAll.ToArray());
                //    cmbSumitUnit.SelectedIndex = 0;
                //}

                Model.DB.tb_User _Temp = _ListSearch.Find(x => x.userName.Equals(cmbUser.Text));

                if (_Temp == null && _ListSearch.Count > 0)
                {
                    cmbUser.SelectedIndex = 1;
                    System.Threading.Thread.Sleep(100);
                    //cmbSumitUnit.TextUpdate -= CmbSumitUnit_TextUpdate;
                    //cmbSumitUnit.Text = _ListSearch[0].userName;
                    //cmbSumitUnit.TextUpdate += CmbSumitUnit_TextUpdate;
                }
            }
            catch (Exception ex)
            {
                string s1 = string.Empty;
            }
        }

        private void CmbUser_TextUpdate(object sender, EventArgs e)
        {
            try
            {
                if (cmbUser.DropDownStyle == ComboBoxStyle.DropDownList) return;
                string stringNew = cmbUser.Text;
                _ListSearch = new List<Model.DB.tb_User>();

                cmbUser.Items.Clear();

                for (int i = 0; i < _ListAll.Count; i++)
                {
                    if (string.IsNullOrWhiteSpace(stringNew) || _ListAll[i].userName.Contains(stringNew))
                        _ListSearch.Add(_ListAll[i]);
                }

                if (_ListSearch.Count <= 0)
                {
                    _ListSearch.AddRange(_ListAll);
                }

                cmbUser.DisplayMember = "userName";
                cmbUser.ValueMember = "userId";
                cmbUser.Items.AddRange(_ListSearch.ToArray());

                cmbUser.SelectionStart = cmbUser.Text.Length;
                cmbUser.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                string s1 = string.Empty;
            }
        }


        //private void CmbUser_TextUpdate(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        if (cmbUser.DropDownStyle == ComboBoxStyle.DropDownList) return;
        //        string stringNew = cmbUser.Text;
        //        _ListSearch = new List<Model.DB.tb_User>();

        //        cmbUser.DataSource = null;

        //        for (int i = 0; i < _ListAll.Count; i++)
        //        {
        //            if (string.IsNullOrWhiteSpace(stringNew) || _ListAll[i].userName.Contains(stringNew))
        //                _ListSearch.Add(_ListAll[i]);
        //        }

        //        if (_ListSearch.Count <= 0)
        //        {
        //            _ListSearch.AddRange(_ListAll);
        //        }

        //        cmbUser.DisplayMember = "userName";
        //        cmbUser.ValueMember = "userId";
        //        cmbUser.DataSource = _ListSearch;

        //        this.Cursor = Cursors.Arrow;
        //        cmbUser.SelectionStart = cmbUser.Text.Length;
        //        cmbUser.DroppedDown = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        string s1 = string.Empty;
        //    }
        //}

        public int SelectedIndex
        {
            get
            {
                return cmbUser.SelectedIndex;
            }
            set
            {
                cmbUser.SelectedIndex = value;
            }
        }
        public object SelectedValue
        {
            get
            {
                return cmbUser.SelectedValue;
            }
            set
            {
                cmbUser.SelectedValue = value;
            }
        }
        public string SelectedText
        {
            get
            {
                return cmbUser.SelectedText;
            }
            set
            {
                cmbUser.SelectedText = value;
            }
        }
        public object SelectedItem
        {
            get
            {
                return cmbUser.SelectedItem;
            }
            set
            {
                cmbUser.SelectedItem = value;
            }
        }
    }
}
